@file:JvmName("CommonExt")
package com.sun.module_base.ext

/**
 *@author:sunc
 *@time :2025/7/21 11:07
 *@desc: 描述
 */
import android.content.ClipData
import android.content.Context
import android.os.Build
import android.provider.Settings
import android.text.Html
import android.text.Spanned
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import com.sun.module_base.app.BaseApp

/**
 * 获取屏幕宽度
 */
val Context.screenWidth
    get() = resources.displayMetrics.widthPixels

/**
 * 获取屏幕高度
 */
val Context.screenHeight
    get() = resources.displayMetrics.heightPixels

/**
 * 判断是否为空 并传入相关操作
 */
inline fun <reified T> T?.notNull(notNullAction: (T) -> Unit, nullAction: () -> Unit = {}) {
    if (this != null) {
        notNullAction.invoke(this)
    } else {
        nullAction.invoke()
    }
}

/**
 * 判断是否为空 并传入相关操作
 */
/*fun <T> Any?.notNull(f: () -> T, t: () -> T): T {
    return if (this != null) f() else t()
}*/




/**
 * dp值转换为px
 */
fun Context.dp2px(dp: Int): Int {
    val scale = resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}

fun dp2px(dpValue: Float): Int {
    return dip2px( BaseApp.getApplicationContext(),dpValue)
}
/**
 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
 */
fun dip2px(context: Context, dpValue: Float): Int {
    val scale: Float = context.resources.displayMetrics.density
    return (dpValue * scale + 0.5f).toInt()
}

/**
 * px值转换成dp
 */
fun px2dp(px: Int): Int {
    return BaseApp.getApplicationContext().px2dp(px)
}
/**
 * px值转换成dp
 */
fun Context.px2dp(px: Int): Int {
    val scale = resources.displayMetrics.density
    return (px / scale + 0.5f).toInt()
}

/**

 * Returns the width of the screen in pixels.
 *
 * @param context The context used to access window services.
 * @return The width of the screen in pixels.
 */
fun Context.getScreenWidth(context: Context): Int {
    // Get the window manager system service
    val windowManager =
        context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    // Return the width of the default display
    return windowManager.defaultDisplay.width
}


/**

 * Retrieves the height of the device's screen in pixels.
 *
 * @param context The context used to access the window service.
 * @return The height of the screen in pixels as an Int.
 */
fun Context.getScreenHeight(context: Context): Int {
    // Get the window system service from the context
    val windowManager =
        context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    // Return the height of the default display
    return windowManager.defaultDisplay.height
}


/**
 * dp值转换为px
 */
fun View.dp2px(dp: Int): Int {
    val scale = resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}
fun Int.dp():Int {
    val scale = BaseApp.getApplicationContext().resources.displayMetrics.density
    return (this * scale + 0.5f).toInt()
}

fun Int.sp():Int {
    val scale =  BaseApp.getApplicationContext().resources.displayMetrics.scaledDensity
    return (this * scale + 0.5f).toInt()
}


/**
 * px值转换成dp
 */
fun View.px2dp(px: Int): Int {
    val scale = resources.displayMetrics.density
    return (px / scale + 0.5f).toInt()
}

/**
 * 复制文本到粘贴板
 */
fun Context.copyToClipboard(text: String, label: String = "JetpackMvvm") {
    val clipData = ClipData.newPlainText(label, text)
    clipboardManager?.setPrimaryClip(clipData)
}

/**
 * 检查是否启用无障碍服务
 */
fun Context.checkAccessibilityServiceEnabled(serviceName: String): Boolean {
    val settingValue =
        Settings.Secure.getString(
            applicationContext.contentResolver,
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
        )
    var result = false
    val splitter = TextUtils.SimpleStringSplitter(':')
    while (splitter.hasNext()) {
        if (splitter.next().equals(serviceName, true)) {
            result = true
            break
        }
    }
    return result
}





fun String.toHtml(flag: Int = Html.FROM_HTML_MODE_LEGACY): Spanned {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml(this, flag)
    } else {
        Html.fromHtml(this)
    }
}